package cn.com.huzo.service.master.impl;

import cn.com.huzo.model.entities.master.LabDipRecordItemEntity;
import cn.com.huzo.repository.master.LabDipRecordItemRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.master.LabDipRecordItemManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 打色记录项目管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("LabDipRecordItemManager")
public class LabDipRecordItemManagerImpl extends GenericManagerImpl<LabDipRecordItemEntity, Long>
        implements LabDipRecordItemManager {

    @Resource(name = "LabDipRecordItemRepository")
    private LabDipRecordItemRepository _dao;

    /**
     * {@inheritDoc}
     *
     */
    @Transactional
    @Override
    public List<LabDipRecordItemEntity> fetchAll() throws Exception {
        return _dao.fetchAll();
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> saveLines(Map<String, ? extends List<LabDipRecordItemEntity>> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();
        int _rowsAffected1 = 0;
        int _rowsAffected2 = 0;
        if (criterion.get("insertList").size() != 0) {
            _rowsAffected1 = _dao.batchInsert(criterion.get("insertList"));
        }
        if (criterion.get("updateList").size() != 0) {
            _rowsAffected2 = _dao.batchUpdate(criterion.get("updateList"));
        }

        if ((_rowsAffected1 + _rowsAffected2) > 0) {
            _result.put("Success", true);
            _result.put("LinesList", _dao.fetchAll());
        } else {
            _result.put("Success", false);
        }
        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Long insert(LabDipRecordItemEntity instance) throws Exception {
        Long key = _dao.insert(instance);
        return key;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        // 删除
        Integer rowsAffected = _dao.delete(MapUtils.getLong(criterion, "ItemId"));
        if (rowsAffected > 0) {
            _result.put("LinesList", _dao.fetchAll());
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }
        return _result;
    }
}
